// Xampler, a MIDI-driven sampler
// Copyright (C) 2006 Nick Thomas
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301 USA.

#ifndef XAMPLER_MESSAGE_BUFFER_HPP
#define XAMPLER_MESSAGE_BUFFER_HPP

// A template that provides a thread-safe FIFO queue over an arbitrary
// type, using JACK's ringbuffer interface.

#include <jack/ringbuffer.h>
#include <glibmm/ustring.h>

namespace xampler
{
    template<class T>
    class MessageBuffer
    {
    public:
        MessageBuffer();
        ~MessageBuffer();

        // Initializes the message buffer, with the ability to hold
        // the given number of messages. On success, returns true. On
        // failure, returns false, and sets the error message
        // appropriately.
        bool init(int size, Glib::ustring &message);

        // Reads a message from the queue into the given location;
        // returns true on success, and false if there are no messages
        // in the queue.
        bool read(T *msg);

        // Adds a message to the queue; returns true on success, and
        // false if there is no room for the message. 
        bool write(T *msg);

    private:
        jack_ringbuffer_t *m_ringbuffer;
    };

    template<class T>
    MessageBuffer<T>::MessageBuffer()
    {
        m_ringbuffer = 0;
    }

    template<class T>
    MessageBuffer<T>::~MessageBuffer()
    {
        if (m_ringbuffer) {
            jack_ringbuffer_free(m_ringbuffer);
        }
    }

    template<class T>
    bool MessageBuffer<T>::init(int size, Glib::ustring &message)
    {
        m_ringbuffer = jack_ringbuffer_create(size * sizeof(T));

        if (m_ringbuffer) {
            return true;
        } else {
            message = "out of memory";
            return false;
        }
    }

    template<class T>
    bool MessageBuffer<T>::read(T *msg)
    {
        if (jack_ringbuffer_read_space(m_ringbuffer) >= sizeof(T)) {
            jack_ringbuffer_read(m_ringbuffer, (char *)msg, sizeof(T));
            return true;
        } else {
            return false;
        }
    }

    template<class T>
    bool MessageBuffer<T>::write(T *msg)
    {
        if (jack_ringbuffer_write_space(m_ringbuffer) >= sizeof(T)) {
            jack_ringbuffer_write(m_ringbuffer, (char *)msg, sizeof(T));
            return true;
        } else {
            return false;
        }
    }
}

#endif
